﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace Core
{
    public class Core
    {
        #region Paths

        public static String getWoWInstallPath()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Blizzard Entertainment\World of Warcraft");
            return key.GetValue("InstallPath").ToString().Remove(key.GetValue("InstallPath").ToString().LastIndexOf(@"\"));
        }

        #endregion

        #region Files & Directories

        public static String getFileVersion(String file)
        {
            return FileVersionInfo.GetVersionInfo(file).FileVersion;
        }

        public static String getProductVersion(String file)
        {
            return FileVersionInfo.GetVersionInfo(file).ProductVersion;
        }

        public static String getDirectories(String path)
        {
            String directories = "";

            foreach (String di in Directory.GetDirectories(path))
            {
                if (!di.Contains(@".svn"))
                    directories += di + "|";
            }

            return directories.Remove(directories.LastIndexOf("|"));
        }

        public static String getFiles(String path)
        {
            String files = "";

            foreach (String di in Directory.GetFiles(path))
            {
                files += di + "|";
            }

            return files.Remove(files.LastIndexOf("|"));
        }

        public static void createFile(String file)
        {
            File.Create(file);
        }

        public static void deleteFile(String file)
        {
            File.Delete(file);
        }

        public static String readFile(String file, FileMode fileMode = FileMode.OpenOrCreate)
        {
            String data = "";

            Stream stream = new FileStream(file, fileMode, FileAccess.ReadWrite, FileShare.ReadWrite);

            StreamReader reader = new StreamReader(stream);
            data = reader.ReadToEnd();
            reader.Close();
            
            stream.Close();

            return data;
        }

        public static void writeFile(String file, String text, FileMode fileMode = FileMode.OpenOrCreate)
        {
            Stream stream = new FileStream(file, fileMode, FileAccess.ReadWrite, FileShare.ReadWrite);

            StreamWriter writer = new StreamWriter(stream);
            writer.Write(text);
            writer.Flush();
            writer.Close();
            
            stream.Close();
        }

        public static void addTextToFile(String file, String text)
        {
            String data = readFile(file);
            writeFile(file, data + text);
        }

        #endregion
    }
}
